package com.doophan.duuchin.service.impl;

import com.doophan.duuchin.dto.PageDTO;
import com.doophan.duuchin.dto.QueryDTO;
import com.doophan.duuchin.mapper.ArticleMapper;
import com.doophan.duuchin.mapper.UserMapper;
import com.doophan.duuchin.model.Article;
import com.doophan.duuchin.service.ArticleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public PageDTO<Article> getList(QueryDTO queryDTO) {
        PageHelper.startPage(queryDTO.getPage(), queryDTO.getLimit());
        List<Article> articleList = articleMapper.selectList(null);
        articleList.forEach( e -> {
            e.setUser(userMapper.selectById(e.getUserId()));
            e.setCoverUrlList(Arrays.asList(e.getCoverUrlStr().split(",")));
        });
        PageInfo pageInfo = new PageInfo<>(articleList);
        PageHelper.clearPage();
        PageDTO pageDTO = PageDTO.builder().total(pageInfo.getTotal()).data(pageInfo.getList()).build();
        return pageDTO;
    }

    @Override
    public Article getInfo(Integer id) {
        return articleMapper.selectById(id);
    }
}
